/*模态框显示隐藏指令*/
yjx.directive('modalDir', ['$rootScope', '$timeout', function modalDir($rootScope, $timeout) {
    return {
        replace: false,
        restrict: 'EAC',
        scope: {
            modalDir: '=modalDir', // 绑定对象
            modalTag: '@modalTag', //指定标签别名
            clickHide: '=clickHide', //是否点击外部隐藏 适用单层，多层模态框组有bug（如支付）
            clickCg: '=clickCg', // 点击外部 改变对象
            modalBg:'@modalBg',
        },
        link: function ($scope, $element, attr) {
            $scope.$watch('modalDir', function (newVal) {
                $scope.modalBg = $scope.modalBg?$scope.modalBg:'.modal-bg';

                var e = $element.closest($scope.modalBg);
                if (e.length == 0) {
                    e = $element;
                }
                //alert( $scope.modalTag);
                var modalTag = $scope.modalTag;
                //close关闭指定tag
                var close = e.find('.e-close');
                //open打开指定tag
                var open = e.find('.open');
                //点击外层改变状态
                e.bind('click', function (event) {
                    if ($scope.clickHide && $(event.target).context.className.search('modal-bg') >= 0) {
                        $timeout(function () {
                            $rootScope.modalStatus = 'modal-hide';
                            $rootScope.modalTag = modalTag;
                            if ($scope.clickCg) {
                                $scope.clickCg = !$scope.clickCg;// 点击外部 改变对象
                            }
                        });
                    }
                });
                //close关闭指定tag改变状态
                close.bind('click', function () {
                    $timeout(function () {
                        $rootScope.modalStatus = 'modal-hide';
                        $rootScope.modalTag = modalTag;

                    });
                });
                //open打开指定tag改变状态
                open.bind('click', function () {
                    $timeout(function () {
                        $rootScope.modalStatus = 'modal-show';
                        $rootScope.modalTag = modalTag;
                    });
                });
            });
        }
    }
}]);